import { CharStreams, CommonTokenStream } from 'antlr4ts';
import { vecMathLexer } from './vecMathLexer';
import { ExprContext, MultExprContext, PrimaryContext, ProgContext, StatContext, vecMathParser } from './vecMathParser';
import { vecMathVisitor } from './vecMathVisitor';
let input = `
x = 3+4
print x * [2, 3, 4]
`;
// Create the lexer and parser
let inputStream = CharStreams.fromString(input);
let lexer = new vecMathLexer(inputStream);
let tokenStream = new CommonTokenStream(lexer);
let parser = new vecMathParser(tokenStream);

// Parse the input, where `compilationUnit` is whatever entry point you defined
let tree = parser.prog();
import { AbstractParseTreeVisitor } from 'antlr4ts/tree/AbstractParseTreeVisitor'
// 自定义访问器处理
class MyVisitor extends AbstractParseTreeVisitor<void> implements vecMathVisitor<void>{

    defaultResult() {
        return 0
    }
    p(msg:string){
        process.stdout.write(msg);
    }
    /**
	 * Visit a parse tree produced by `vecMathParser.prog`.
	 * @param ctx the parse tree
	 * @return the visitor result
	 */
	visitProg (ctx: ProgContext){
        
    };

	/**
	 * Visit a parse tree produced by `vecMathParser.stat`.
	 * @param ctx the parse tree
	 * @return the visitor result
	 */
	visitStat (ctx: StatContext){
        
    };

	/**
	 * Visit a parse tree produced by `vecMathParser.expr`.
	 * @param ctx the parse tree
	 * @return the visitor result
	 */
	visitExpr (ctx: ExprContext){
        
    }

	/**
	 * Visit a parse tree produced by `vecMathParser.multExpr`.
	 * @param ctx the parse tree
	 * @return the visitor result
	 */
	visitMultExpr (ctx: MultExprContext){
        
    }

	/**
	 * Visit a parse tree produced by `vecMathParser.primary`.
	 * @param ctx the parse tree
	 * @return the visitor result
	 */
	visitPrimary (ctx: PrimaryContext){
        if(ctx.ID()){
            this.p(ctx.ID()?.text as string);
        }else if(ctx.INT()){
            this.p(ctx.INT()?.text as string)
        }else {
            // []
            this.p("[");
            this.p(',');
            this.p(']')
        }
    }
}

let v = new MyVisitor();
v.visit(tree);